package org.summerframework.component.security.session.websocket;

import org.apache.commons.collections4.MapUtils;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.util.LinkedMultiValueMap;

import java.util.HashMap;
import java.util.Map;

import static org.springframework.messaging.support.NativeMessageHeaderAccessor.NATIVE_HEADERS;

public class WebsocketUtils {
    public static Map<String, Object> sendToUser(Map<String, Object> map) {
        map.put(NATIVE_HEADERS, new LinkedMultiValueMap<String, String>());

        return map;
    }

    public static Map<String, Object> sendToUser(String sessionId) {
        return sendToUser(sessionId, null);
    }

    public static Map<String, Object> sendToUser(String sessionId, Map<String, Object> header) {
        Map<String, Object> map = new HashMap<>(16);

        map.put(SimpMessageHeaderAccessor.SESSION_ID_HEADER, sessionId);
        sendToUser(map);

        if (!MapUtils.isEmpty(header)) {
            map.putAll(header);
        }

        return map;
    }
}
